package com.taxshare.tools.token;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 获取用户信息
 *
 */
public class UserInfoUtil {
    private static final String BLANK = "";
    /**
     * 获取用户userId
     */
    public static String getUserID() {
        if (RequestContextHolder
                .getRequestAttributes() == null) {
            return BLANK;
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        return String.valueOf(request.getAttribute("userId"));
    }

    /**
     * 获取用户的登陆名称
     */
    public static String getUserLoginName() {
        if (RequestContextHolder
                .getRequestAttributes() == null) {
            return BLANK;
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        return String.valueOf(request.getAttribute("userLoginName"));
    }

    /**
     * 获取用户nickName
     */
    public static String getUserNickName() {
        if (RequestContextHolder
                .getRequestAttributes() == null) {
            return BLANK;
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        return String.valueOf(request.getAttribute("userNickName"));
    }

    /**
     * 获取集团编码
     */
    public static String getTenantId() {
        if (RequestContextHolder
                .getRequestAttributes() == null) {
            return BLANK;
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        return String.valueOf(request.getAttribute("tenantId"));
    }

    /**
     * 获取角色编码
     */
    public static String getRoleId() {
        if (RequestContextHolder
                .getRequestAttributes() == null) {
            return BLANK;
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        return String.valueOf(request.getAttribute("roleId"));
    }

    /**
     * 获取拓展字段
     */
    public static Map<String, Object> getExpandField() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        return (Map<String, Object>) request.getAttribute("expandField");
    }

    /**
     * 获取用户类型:1普通用户 2集团用户 3平台用户 说明:如果获取不到值则是个人用户返回 0
     */
    public static String getUserType() {
        if (RequestContextHolder
                .getRequestAttributes() == null) {
            return BLANK;
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        return String.valueOf(request.getAttribute("userType"));
    }

}
